$(function(){
	
	$('#admin-side').on('click', '.layui-nav-item>a',function(){
		var childLen = $(this).next('dl.layui-nav-child').length;
		if(childLen !== 0){
			if($(this).parent().hasClass('layui-nav-itemed')){
				$(this).parent().removeClass('layui-nav-itemed');
			} else {
				$(this).parent().addClass('layui-nav-itemed');
			}
		}
	});

	

	
	$('.header-top-nav').on('click','.nav-box-link',function(){
		var self = this;
		$(this).addClass('active').siblings().removeClass('active');
		var leftsideW = 200;
		$('#admin-side').animate({
            width: leftsideW - 1 +'px'
        });
        $('#admin-footer').animate({
            left: leftsideW + "px"
        });
        $("#admin-body").animate({
        	"marginLeft": leftsideW + "px"
        });
		$.ajax({
			type: "POST",
            url: "http://sailorw1.6655.la:29508/sz-core/sys/sysauth/menu/"+$(this).attr('data-id'),
            dataType:"json",
            success:function(data){
    			console.log(data);
    			var leftNavTitle = '<div class="layui-nav-item-title"><a><i class="fa fa-cog icon-maright8" data-icon="fa-cubes"></i><cite>'+$(self).find('dd').html()+'</cite></a></div><span class="toggle-icon" id="admin-side-toggle"></span>';
             	//var str = '<ul class="layui-nav layui-nav-tree"><li class="layui-nav-item-title"><a href="javascript:;"><i class="fa fa-cog icon-maright5" data-icon="fa-cubes"></i><cite>'+$(self).find('dd').html()+'</cite></a></li>';
			 	var authOutList = $('<div class="scrollbox"><div class="layui-side-scroll" id="admin-navbar-side"><ul class="layui-nav layui-nav-tree"></ul></div></div>');
			 	var content = '';
				for(var i in data.authList){
	
					if(data.authList[i].children.length == 0 ){
						content += '<li class="layui-nav-item"><a href="javascript:;" class="addtab-active" data-title="'+data.authList[i].authName+'" data-id="'+data.authList[i].id+'" data-href="'+data.authList[i].href+'" data-url="/sz-core'+data.authList[i].href+'" data-type="tabAdd"><i class="fa fa-file-text-o  icon-maright8" aria-hidden="true"></i><cite>'+data.authList[i].authName+'</cite></a></li>';
					}else{
						var strChild = '';
						for(var j in data.authList[i].children){
							strChild += '<dd><a href="javascript:;" class="addtab-active"  data-title="'+data.authList[i].children[j].authName+'" data-id="'+data.authList[i].children[j].id+'" data-href='+data.authList[i].children[j].href+'" data-url="/sz-core'+data.authList[i].children[j].href+'" data-type="tabAdd"><i class="fa fa-navicon icon-maright5" aria-hidden="true"></i><cite>'+data.authList[i].children[j].authName+'</cite></a></dd>';
						}
						content += '<li class="layui-nav-item"><a href="javascript:;" data-id="'+data.authList[i].id+'" data-url="/sz-core'+data.authList[i].children[j].href+'" data-type="tabAdd"><i class="fa fa-file-text-o icon-maright8" aria-hidden="true"></i><cite>'+data.authList[i].authName+'</cite><span class="layui-nav-more"></span></a><dl class="layui-nav-child">'+strChild+'</dl></li>';
					}
				 	
				 }
				content = '<div class="scrollbox"><div class="layui-side-scroll" id="admin-navbar-side"><ul class="layui-nav layui-nav-tree">' + content + '</ul></div></div>';
			 	$('#admin-side').html(leftNavTitle + content);
			}
			
		});
	});
	
	$.ajax({
		type: "POST",
		url: "http://sailorw1.6655.la:29508/sz-core/sys/sysauth/catalog",
		dataType: "json",
		success: function(data) {
	
			var data = data;
			var str = "";
			for(var i in data) {
				if(i == 0) {
					str += '<a href="javascript:;" class="nav-box-link active" data-id=' + data[i].id + '><dl><dt><i class="fa fa-user-o" ></i></dt><dd>' + data[i].authName + '</dd></dl></a>';
				} else {
					str += '<a href="javascript:;" class="nav-box-link" data-id=' + data[i].id + '><dl><dt><i class="fa fa-user-o" ></i></dt><dd>' + data[i].authName + '</dd></dl></a>';
				}
	
			}
			$('#navtop-box').html(str);
			$('#navtop-box .active').trigger('click');
		}
	});
});